Skip to content

Conversation

@neveler
Copy link
Contributor

@neveler neveler commented Oct 3, 2025

支持多语言(插件)

该方案依赖我向上游提交的一个 PR 其作用是使 UI 语言能够优先依据 page.locale 进行判断。

介绍

二级后缀自动作为 page.locale

插件会根据文件名的二级后缀为文章指定 page.locale 详情如下:

  1. _config.yml 中配置字符串类型参数 locale 作为系统的默认语言区域代码
  2. _config.yml 中配置字符串数组类型参数 locales 例如 locales: [zh-Hans, zh-Hant, en] 表示支持的语言区域代码列表,其中可以省略默认语言区域代码

插件会在 site pre_render 时期遍历所有页面 Page 与文档 Document 并根据文件名结合上面两个参数判断当前文档的语言区域代码。以配置 locale: zh-Hanslocales: [zh-Hant, en] 为例 index.md index.es.md 的文件名中均未命中配置列表中受支持的代码,因此使用默认代码 zh 作为 page.localehelp.en.mdfaq.zh-Hant.md 会分别使用 enzh-Hant 作为 page.locale

自动配置多语言页面 page.permalink

插件要求内容必须存在默认语言版本,无默认语言版本的内容不会被插件处理,等价于 i18n: false 配置。

插件会根据默认语言页面的 page.permalink 自动为多语言页面配置 page.permalink/{locale}/{default_permalink} 假设 /test/a.md 对应的 page.permalink/test-a.html 那么 /test/a.en.md 对应的 page.permalink 则为 /en/test-a.html

为不存在的语言版本提供回退页面

如果一篇文章并没有完整的支持所有的语言版本,在用户访问不存在的语言版本页面时用户会看到一个回退页面而非 404 页面。用户可以通过 _data/plugins/i18n/fallback.yml 文件配置回退规则,详情如下:

# 若 lzh 版本页面不存在,则回退使用 zh-Hant 版本
lzh: zh-Hant

# 回退规则支持嵌套:
# 若 lzh 与 zh-Hant 版本页面均不存在,则使用 zh-Hans 版本页面回退
zh-Hant: zh-Hans

# 对未配置回退规则的语言区域代码,使用默认语言区域代码作为回退

其它

需要启用多语言的页面需要配置 i18n: true 如下所示:

---
title: xxx
i18n: true
---

xxx

待做清单

多语言支持

  • 颜色模式切换器
  • 预览构建信息
  • 站点信息
    部分完成,已支持 site.title site.description site.date_format
  • 署名信息
  • 404 页面
  • 语言切换器
  • 自动生成的页面
    部分完成,已支持 feed sitemap
  • 多语言页面锚点支持相互转化
    /test.html: #帮助
    /en/test.html: #help
  • 链接站内多语言文章优先使用与本文相同语言的链接
    /test.html: [demo](/_docs/demo.md) => /demo.html
    /en/test.html: [demo](/_docs/demo.md) => /en/demo.html

待合并 PR 的多语言支持

其它

  • 集合内容的上一页与下一页按钮会切换到非当前语言的内容

关联 ISSUES

预览

语言 链接
简体中文 https://hmcl-docs.github.io/HMCL-docs/PR1/
繁体中文 https://hmcl-docs.github.io/HMCL-docs/PR1/zh-Hant/
English https://hmcl-docs.github.io/HMCL-docs/PR1/en/

@zkitefly
Copy link
Member

zkitefly commented Oct 5, 2025

assets/img/hmcl/自动管理所有驱动器分页文件大小.webp
index.json

以上文件保留,其余可以删除

@neveler neveler marked this pull request as draft October 5, 2025 10:22
@zkitefly
Copy link
Member

zkitefly commented Oct 6, 2025

现在会自动获取所有可见集合中的内容生成 index.json 文件,但有一个问题当前会对集合中的所有文章生成索引数据,是否需要排除一些文章?生成索引的逻辑:获取所有布局为 document-collection 且指定了 collection 属性的页面,通过这些页面的 collection 属性获取指定集合的所有文章并提取文章的 title description url 属性进而生成 json 索引文件。

将 下载 集合的文章排除就好

@neveler neveler marked this pull request as ready for review October 7, 2025 05:57
@neveler neveler mentioned this pull request Oct 11, 2025
1 task
@neveler neveler marked this pull request as draft October 14, 2025 00:59
@neveler neveler changed the title 支持多语言 尝试通过插件的方式支持多语言 Nov 15, 2025
@neveler neveler changed the title 尝试通过插件的方式支持多语言 尝试通过插件支持多语言 Nov 15, 2025
@neveler neveler marked this pull request as ready for review November 16, 2025 03:04
@neveler neveler marked this pull request as draft November 16, 2025 12:37
@neveler neveler force-pushed the dev-i18n branch 2 times, most recently from 7aa9e0f to a05d6e8 Compare November 16, 2025 14:44
@neveler neveler changed the title 尝试通过插件支持多语言 尝试支持多语言(插件) Nov 16, 2025
@neveler neveler force-pushed the dev-i18n branch 11 times, most recently from af0ed34 to e30a31c Compare November 18, 2025 15:38
@neveler neveler force-pushed the dev-i18n branch 5 times, most recently from baf5b21 to dd80d7c Compare November 19, 2025 05:26
@neveler neveler marked this pull request as ready for review November 19, 2025 06:20
@neveler neveler changed the title 尝试支持多语言(插件) 支持多语言(插件) Nov 19, 2025
@neveler neveler force-pushed the dev-i18n branch 4 times, most recently from 886ac95 to ec39721 Compare November 19, 2025 23:47
@neveler neveler force-pushed the dev-i18n branch 2 times, most recently from de3df7d to 565df46 Compare November 20, 2025 05:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants